package aos.ydksxt.service.jcxxcj;

import aos.framework.core.typewrap.Dto;
import aos.framework.core.typewrap.Dtos;
import aos.framework.core.utils.AOSJson;
import aos.framework.core.utils.AOSUtils;
import aos.framework.web.router.HttpModel;
import aos.ydksxt.dao.DjRyxx;
import aos.ydksxt.dao.DjRyxxKz;
import aos.ydksxt.service.common.CommonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人员基础信息采集服务
 * Created by Stiofan on 2016/12/15.
 */
@Service
public class RyjcxxcjService extends CommonService {

    @Override
    @Transactional
    public void initView(HttpModel httpModel) {
        httpModel.setViewPath("/ydksxt/jcxxcj/ryjcxxcj.jsp");
    }

    /**
     * 查询人员相关信息
     *
     * @param httpModel
     */
    @Override
    @Transactional
    public void queryRyjcxx(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto queryParam = Dtos.newDto();
        queryParam.put("nsrsbh", inDto.get("nsrsbh"));
        queryParam.put("shxydm", inDto.get("shxydm"));
        queryParam.put("zzjgdm", inDto.get("zzjgdm"));
        queryParam.put("djxh", inDto.getBigDecimal("djxh"));
        DjRyxx djRyxx = new DjRyxx();
        DjRyxxKz djRyxxKz = new DjRyxxKz();
        List ydksResults = persistenceDAO.findAllByHql("from DjRyxx a,DjRyxxKz b where a.djxh=b.djxh and (a.nsrsbh=:nsrsbh or b.shxydm=:shxydm or b.zzjgdm=:zzjgdm or a.djxh=:djxh) and a.yxbz='Y'", queryParam);
        List newYdksResults = new ArrayList();
        Dto outDto = Dtos.newOutDto();
        if (ydksResults.size() != 0) {
            for (Object ydksResult : ydksResults) {
                Object[] objects = (Object[]) ydksResult;
                for (Object object : objects) {
                    if (object instanceof DjRyxx) {
                        djRyxx = (DjRyxx) object;
                    } else if (object instanceof DjRyxxKz) {
                        djRyxxKz = (DjRyxxKz) object;
                    }
                }
                if (inDto.getBigDecimal("djxh") != null && inDto.getBigDecimal("djxh").equals(djRyxx.getDjxh())) {
                    outDto.putAll((Map) AOSUtils.object2Map(djRyxx));
                    outDto.putAll((Map) AOSUtils.object2Map(djRyxxKz));
                    if (!httpModel.getUserModel().getAccount().equals(djRyxx.getSsglyDm()) && djRyxx.getSsglyDm() != null) {//判断税收管理员代码与当前操作人员不一致，给我提示
                        checkSsgly(outDto, djRyxx.getSsglyDm());
                    }
                    outDto.put("lxrdz", getFullAddress(djRyxx.getLxrdzXzqhdsDm(), djRyxx.getLxrdzXzqhxqDm(), djRyxx.getLxrdzJdxzDm(), djRyxx.getLxrdzSqjqDm(), djRyxx.getLxrdzDlxdDm(), djRyxx.getLxrdzLh()));
                    httpModel.setOutMsg(AOSJson.toJson(outDto));
                    return;
                }
                Map tmpMap = new HashMap();
                tmpMap.putAll((Map) AOSUtils.object2Map(djRyxx));
                tmpMap.putAll((Map) AOSUtils.object2Map(djRyxxKz));
                tmpMap.put("lxrdz", getFullAddress(djRyxx.getLxrdzXzqhdsDm(), djRyxx.getLxrdzXzqhxqDm(), djRyxx.getLxrdzJdxzDm(), djRyxx.getLxrdzSqjqDm(), djRyxx.getLxrdzDlxdDm(), djRyxx.getLxrdzLh()));
                newYdksResults.add(tmpMap);
            }
            httpModel.setOutMsg(AOSJson.toGridJson(newYdksResults));
        } else {
            if(StringUtils.isEmpty(queryParam.getString("djxh"))){
                queryParam.put("djxh",0);
            }
            List jsResults = persistenceDAO.findAllBySql("select nvl(xx.shxydm,xx.nsrsbh) nsrsbh,\n" +
                    "       xx.shxydm,\n" +
                    "       xx.zzjg_dm zzjgdm,\n" +
                    "       xx.fddbrxm,\n" +
                    "       xx.nsrmc,\n" +
                    "       xx.kzztdjlx_dm,\n" +
                    "       xx.djzclx_dm,\n" +
                    "       xx.fddbrsfzjlx_dm,\n" +
                    "       xx.fddbrsfzjhm,\n" +
                    "       xx.zgswj_dm zgswjg_dm,\n" +
                    "       xx.zgswskfj_dm zgswskfj_dm,\n" +
                    "       kz.fddbrgddh,\n" +
                    "       kz.fddbryddh,\n" +
                    "       kz.cwfzrxm,\n" +
                    "       kz.cwfzrsfzjzl_dm cwfzrsfzjlx_dm,\n" +
                    "       kz.cwfzrsfzjhm,\n" +
                    "       kz.cwfzrgddh,\n" +
                    "       kz.cwfzryddh,\n" +
                    "       (select qh.sjxzqhsz_dm\n" +
                    "          from hx_dm_qg.dm_gy_xzqh qh\n" +
                    "         where qh.xzqhsz_dm = xx.scjydzxzqhsz_dm) scjydz_xzqhds_dm,\n" +
                    "       xx.scjydzxzqhsz_dm scjydz_xzqhxq_dm,\n" +
                    "       xx.jdxz_dm scjydz_jdxz_dm,\n" +
                    "       xx.scjydz scjydz_sq,\n" +
                    "       kz.scjydlxdh scjydz_lxdh,\n" +
                    "       (select qh.sjxzqhsz_dm\n" +
                    "          from hx_dm_qg.dm_gy_xzqh qh\n" +
                    "         where qh.xzqhsz_dm = xx.zcdzxzqhsz_dm) zcdz_xzqhds_dm,\n" +
                    "       xx.zcdzxzqhsz_dm zcdz_xzqhxq_dm,\n" +
                    "       xx.jdxz_dm zcdz_jdxz_dm,\n" +
                    "       xx.zcdz zcdz_sq,\n" +
                    "       xx.zcdz zcdz_jsdz,\n" +
                    "       kz.zcdlxdh zcdz_lxdh,\n" +
                    "       xx.sjgsdq,\n" +
                    "       to_char(xx.djxh) djxh,\n" +
                    "       xx.ssgly_dm,\n" +
                    "       xx.fddbrxm lxrxm,\n" +
                    "       xx.fddbrsfzjlx_dm lxrsfzjlx_dm,\n" +
                    "       xx.fddbrsfzjhm lxrsfzjhm,\n" +
                    "       kz.fddbrgddh lxrgddh,\n" +
                    "       kz.fddbryddh lxryddh,\n" +
                    "       (select qh.sjxzqhsz_dm\n" +
                    "          from hx_dm_qg.dm_gy_xzqh qh\n" +
                    "         where qh.xzqhsz_dm = xx.scjydzxzqhsz_dm) lxrdz_xzqhds_dm,\n" +
                    "       xx.scjydzxzqhsz_dm lxrdz_xzqhxq_dm,\n" +
                    "       xx.jdxz_dm lxrdz_jdxz_dm,\n" +
                    "       xx.scjydz lxrdz_sq,\n" +
                    "       'jssj' sjlybz\n" +
                    "  from hx_dj.dj_nsrxx xx, hx_dj.dj_nsrxx_kz kz\n" +
                    " where xx.djxh = kz.djxh\n" +
                    "   and xx.yxbz = 'Y'\n" +
                    "   and xx.kzztdjlx_dm in ('1110', '1120', '1500')\n" +
                    "   and (xx.nsrsbh = :nsrsbh or xx.shxydm = :shxydm or xx.zzjg_dm = :zzjgdm or\n" +
                    "       xx.djxh = :djxh)\n" +
                    "union all\n" +
                    "select r.nsrsbh,\n" +
                    "       null shxydm,\n" +
                    "       null zzjgdm,\n" +
                    "       null fddbrxm,\n" +
                    "       r.xm nsrmc,\n" +
                    "       null kzztdjlx_dm,\n" +
                    "       null djzclx_dm,\n" +
                    "       null fddbrsfzjlx_dm,\n" +
                    "       null fddbrsfzjhm,\n" +
                    "       null zgswjg_dm,\n" +
                    "       null zgswskfj_dm,\n" +
                    "       null fddbrgddh,\n" +
                    "       null fddbryddh,\n" +
                    "       null cwfzrxm,\n" +
                    "       null cwfzrsfzjlx_dm,\n" +
                    "       null cwfzrsfzjhm,\n" +
                    "       null cwfzrgddh,\n" +
                    "       null cwfzryddh,\n" +
                    "       null scjydz_xzqhds_dm,\n" +
                    "       null scjydz_xzqhxq_dm,\n" +
                    "       null scjydz_jdxz_dm,\n" +
                    "       null scjydz_sq,\n" +
                    "       null scjydz_lxdh,\n" +
                    "       null zcdz_xzqhds_dm,\n" +
                    "       null zcdz_xzqhxq_dm,\n" +
                    "       null zcdz_jdxz_dm,\n" +
                    "       null zcdz_sq,\n" +
                    "       null zcdz_jsdz,\n" +
                    "       null zcdz_lxdh,\n" +
                    "       r.sjgsdq,\n" +
                    "       to_char(r.djxh) djxh,\n" +
                    "       r.lrr_dm ssgly_dm,\n" +
                    "       r.xm lxrxm,\n" +
                    "       r.sfzjlx_dm lxrsfzjlx_dm,\n" +
                    "       r.sfzjhm lxrsfzjhm,\n" +
                    "       null lxrgddh,\n" +
                    "       null lxryddh,\n" +
                    "       null lxrdz_xzqhds_dm,\n" +
                    "       null lxrdz_xzqhxq_dm,\n" +
                    "       null lxrdz_jdxz_dm,\n" +
                    "       null lxrdz_sq,\n" +
                    "       'jssj' sjlybz\n" +
                    "  from gs_cxtj.dj_zrr r\n" +
                    " where r.yxbz = 'Y'\n" +
                    "   and (r.nsrsbh = :nsrsbh or r.djxh = :djxh)\n", queryParam);
            if (jsResults.size() != 0) {
                for (Object object : jsResults) {
                    Map map = (Map) object;
                    outDto.put("lxrdz", getFullAddress((String) map.get("lxrdzXzqhdsDm"), (String) map.get("lxrdzXzqhxqDm"), (String) map.get("lxrdzJdxzDm"), (String) map.get("lxrdzSqjqDm"), (String) map.get("lxrdzDlxdDm"), (String) map.get("lxrdzLh")));
                    if (inDto.getString("djxh").equals(map.get("djxh"))) {
                        outDto.putAll((Map) AOSUtils.object2Map(map));
                        outDto.setAppCode("suc");
                        httpModel.setOutMsg(AOSJson.toJson(outDto));
                        return;
                    }
                }
                httpModel.setOutMsg(AOSJson.toGridJson(jsResults));
            }
        }

    }
    @Transactional
    public void saveRyjcxx(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto queryParam = Dtos.newDto();
        queryParam.put("djxh", inDto.getBigDecimal("djxh"));
        List ydksResults = persistenceDAO.findAllByHql("from DjRyxx a,DjRyxxKz b where a.djxh=b.djxh and a.djxh=:djxh and a.yxbz='Y'", queryParam);
        DjRyxx djRyxx = new DjRyxx();
        DjRyxxKz djRyxxKz = new DjRyxxKz();
        if (ydksResults.size() != 0) {
            Object[] objects = (Object[]) ydksResults.get(0);
            for (Object object : objects) {
                if (object instanceof DjRyxx) {
                    djRyxx = (DjRyxx) object;
                } else if (object instanceof DjRyxxKz) {
                    djRyxxKz = (DjRyxxKz) object;
                }
            }
        }
        AOSUtils.copyProperties(inDto, djRyxx);
        AOSUtils.copyProperties(inDto, djRyxxKz);
        djRyxx.setYxbz("Y");
        djRyxxKz.setYxbz("Y");
        djRyxx.setSjgsdq(String.valueOf(httpModel.getUserModel().getOrg_id()));
        djRyxxKz.setSjgsdq(String.valueOf(httpModel.getUserModel().getOrg_id()));
        if (ydksResults.size() == 0) {
            if (djRyxx.getDjxh() == null) {//如果登记序号为空表示为新增人员，如果不为空则表示从金三获取数据
                djRyxx.setDjxh(AOSUtils.getRandomDjxh());
                djRyxx.setSjlybz("ydks");
            } else {//当为金三获取数据时将sjly标志改为jssj
                djRyxx.setSjlybz("jssj");
            }
            djRyxx.setLrrDm(getCzryDm(httpModel));
            djRyxx.setLrrq(AOSUtils.getDateTime());
            djRyxxKz.setDjxh(djRyxx.getDjxh());
            djRyxxKz.setLrrDm(getCzryDm(httpModel));
            djRyxxKz.setLrrq(AOSUtils.getDateTime());
        } else {
            djRyxx.setXgrDm(getCzryDm(httpModel));
            djRyxx.setXgrq(AOSUtils.getDateTime());
            djRyxxKz.setXgrDm(getCzryDm(httpModel));
            djRyxxKz.setXgrq(AOSUtils.getDateTime());
        }
        Dto outDto = Dtos.newOutDto();
        try {
            persistenceDAO.saveOrUpdate(djRyxx);
            persistenceDAO.saveOrUpdate(djRyxxKz);
            outDto.setAppCode("suc");
        } catch (Exception e) {
            outDto.setAppCode("err");
            outDto.setAppMsg(e.getMessage());
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
}
